package com.fumin.controller;

import com.fumin.common.JsonData;
import com.fumin.model.SysUser;
import com.fumin.param.RoleParam;
import com.fumin.service.*;
import com.fumin.util.StringUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 角色controller
 * 用户和角色,角色与权限,用户与权限,以角色联系起来的,所以处理的时候在角色的controller中处理
 * @Author xiayuxuanmin
 * @Date 2019/11/13
 * @Description
 */
@Controller
@RequestMapping("/sys/role")
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysTreeService sysTreeService;
    @Resource
    private SysRoleAclService sysRoleAclService;
    @Resource
    private SysRoleUserService sysRoleUserService;
    @Resource
    private SysUserService sysUserService;

    //项目默认以JSON返回的，用.json结尾，返回页面的以.page结尾
    @RequestMapping("/role.page")
    public ModelAndView page(){
        //在视图解析器中，根据prefix和suffix查找对应的JSP文件，这里是查找/WEB-INF下面的/views/role.jsp文件
        return new ModelAndView("role");
    }

    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData saveRole(RoleParam param){
        sysRoleService.save(param);
        return JsonData.success();
    }

    @RequestMapping("/list.json")
    @ResponseBody
    public JsonData list(){
        return JsonData.success(sysRoleService.getAll());
    }

    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData updateAcl(RoleParam param){
        sysRoleService.update(param);
        return JsonData.success();
    }

    /**
     * 获取指定角色的所有权限树结构的数据
     * @param roleId
     * @return
     */
    @RequestMapping("/roleTree.json")
    @ResponseBody
    public JsonData roleTree(@RequestParam("roleId") int roleId){
        return JsonData.success(sysTreeService.roleTree(roleId));
    }

    /**
     * 修改角色的权限点
     * @param roleId 角色ID
     * @param aclIds 权限点ID集合
     * @return
     */
    @RequestMapping("/changeAcls.json")
    @ResponseBody
    public JsonData changeAcls(@RequestParam("roleId") int roleId,@RequestParam(value = "aclIds", required = false, defaultValue = "") String aclIds){
        List<Integer> aclIdList = StringUtil.splitToListInt(aclIds);
        sysRoleAclService.changeRoleAcls(roleId,aclIdList);
        return JsonData.success();
    }

    /**
     * 修改角色对应的用户
     * @param roleId
     * @param userIds
     * @return
     */
    @RequestMapping("/changeUsers.json")
    @ResponseBody
    public JsonData changeUsers(@RequestParam("roleId") int roleId,@RequestParam(value = "userIds", required = false, defaultValue = "") String userIds){
        List<Integer> userIdList = StringUtil.splitToListInt(userIds);
        sysRoleUserService.changeRoleUsers(roleId,userIdList);
        return JsonData.success();
    }

    @RequestMapping("/users.json")
    @ResponseBody
    public JsonData users(@RequestParam("roleId") int roleId){
        //获取已选用户列表(不管用户是不是已经禁用,只要选中,都会进行显示)
        List<SysUser> selectedUserList = sysRoleUserService.getListByRoleId(roleId);
        //获取所有用户列表
        List<SysUser> allUserList = sysUserService.getAll();
        List<SysUser> unselectedUserList = Lists.newArrayList();
        //把已选用户的ID组成一个Set集合
        Set<Integer> selectedUserIdSet = selectedUserList.stream().map(sysUser -> sysUser.getId()).collect(Collectors.toSet());
        for (SysUser sysUser : allUserList) {
            if (sysUser.getStatus() == 1 && !selectedUserIdSet.contains(sysUser.getId())) {
                //如果状态可用,并且没有在已选用户里面,那么放入到未选用户列表中
                unselectedUserList.add(sysUser);
            }
        }
        //如果已选用户被禁用了,是否显示,这里考虑业务是否需要过滤,这里使用stream()的filter列出过滤条件
        //selectedUserList = selectedUserList.stream().filter(sysUser -> sysUser.getStatus() != 1).collect(Collectors.toList());
        Map<String,List<SysUser>> map = Maps.newHashMap();
        map.put("selected",selectedUserList);
        map.put("unselected",unselectedUserList);
        return JsonData.success(map);
    }
}
